package com.qk.management.controller;

import com.qk.common.Result;

import com.qk.management.aop.anno.LogAnno;
import com.qk.utils.AliYunOSSOperators;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;
import java.util.UUID;

/**
 *
 * @author lyl
 * @created 2025/10/6 15:30
 * @description
 *
 */

@Slf4j
@RestController
@SuppressWarnings("all")
public class UploadController {

    @Autowired
    private AliYunOSSOperators aliyunOSSOperator;

    @LogAnno
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        try {
            Objects.requireNonNull(image, "上传文件为空");
            Objects.requireNonNull(image.getOriginalFilename(), "文件名不存在");
            log.info("文件上传开始：{}", image.getOriginalFilename());
            return Result.success(aliyunOSSOperator.upload(image.getBytes(), UUID.randomUUID().toString().concat(image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf(".")))));
        } catch (Exception e) {
            log.error("文件上传失败：{}", e.getMessage());
            return Result.error("文件上传失败");
        }
    }
}
